// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    markers:[
      {
        longitude:121.46924,
        latitude:31.22986,
        title:"shabi",
        iconPath:"/image/dingwei.png",
        width:30,
        height:30
      },
      {
        longitude:121.46980,
        latitude:31.22996,
        title:"shabi",
        iconPath:"/image/dingwei.png",
        width:30,
        height:30
      }
    ],

    secondHandHouse:[],
    limit:5,
    page:1,
    isShowLine:false,
    houseCount:'',
    city:"",
    searchValue:'',
    grids:[
      {"image":'http://hhh.guobaochunlove.xyz/feiji.png',"text":'消息','skip':'message'},
      {"image":'http://hhh.guobaochunlove.xyz/dianpu.png',"text":'求租','skip':'wanted'},
      {"image":'http://hhh.guobaochunlove.xyz/fangzi.png',"text":'求购','skip':'buy'},
      {"image":'http://hhh.guobaochunlove.xyz/dengta.png',"text":'排行榜','skip':'brokercircle'},
      {"image":'http://hhh.guobaochunlove.xyz/caifang.png',"text":'地图找房','skip':'mapFindHouse'},
      {"image":'http://hhh.guobaochunlove.xyz/dianpupinggu.png',"text":'团购','skip':'grouppurchase'},
      {"image":'http://hhh.guobaochunlove.xyz/jingbei.png',"text":'楼市圈','skip':'build'},
      {"image":'http://hhh.guobaochunlove.xyz/haitun.png',"text":'战绩','skip':'record'},
      {"image":'http://hhh.guobaochunlove.xyz/haitun.png',"text":'资讯','skip':'record'},
      {"image":'http://hhh.guobaochunlove.xyz/haitun.png',"text":'经纪人','skip':'record'}
    ]
  },

 
  onLoad() {
    /**
     * 获取二手房列表
     */
    let limit = this.data.limit
    let page = this.data.page
    
    this.getHouseList([],limit,page)
  },


    /**
     * 获取二手房列表
     */
  getHouseList(oldGHouseList=[],limit,page,searchValue='',city='北京市'){
    wx.request({
      url: 'http://www.house.com/index.php/api/house',
      data:{
        limit:limit,
        page:page,
        search:searchValue,
        city:wx.getStorageSync('city')?wx.getStorageSync('city'):'北京市'
      },
      success:res=>{
        //房源老数据
      //let oldGHouseList = this.data.secondHandHouse
       if(res.data.code==200){
        wx.hideLoading({
          
        })
        let newHouselist = oldGHouseList.concat(res.data.data.houseList)
         this.setData({
          secondHandHouse:newHouselist,
          page:page,
          houseCount:res.data.data.houseCount
         })
       }else{
        wx.hideLoading({
          
        })
         console.log(8002)
        this.setData({
          secondHandHouse:oldGHouseList,
          page:1,
          houseCount:0
         })
       }
      }
    })
  },

  /**
   * 下拉分页 数据加载完不发送请求
   */
  onReachBottom(){
    wx.showLoading({
      title: '数据加载中',
    })
    
    let page = this.data.page+1
    let limit = this.data.limit
    let houseCount = this.data.houseCount
    let oldGHouseList = this.data.secondHandHouse
    let searchValue = this.data.searchValue
    if ((page-1)*limit>houseCount) {
      wx.hideLoading({
          
      })
      this.setData({
        isShowLine:true
      })
      return
    }    
    this.getHouseList(oldGHouseList,limit,page,searchValue)
  },

 /**
  * 选择城市
  */
 selectCity(){
  wx.navigateTo({
    url: '/pages/addressInfo/addressInfo',
  })
  
 },

onReady(){
 this.swiper();
},
 onShow(){
  let city = wx.getStorageSync('city')


  console.log(city)
  let page = this.data.page;
  let limit = this.data.limit
  this.getHouseList([],limit,page,'',city)
    if(city==''){
      this.setData({
        city:'北京市'
      })
    }else{
      this.setData({
        city
      })
    }
    this.swiper();
 },
 swiper(){
  wx.getStorage({
    key:"city",
    success:(res)=>{
      let address=res.data;
      wx.request({
        url: 'http://www.house.com/index.php/swiper',
        data:{address:address},
        success:(ress)=>{
          // console.log(ress);
          let data=ress.data.data;
          this.setData({
            data:data
          })
        }
      })
    },
    fail:()=>{
    wx.setStorage({
      key:'city',
      data:"北京市"
    })
    
    }
  })
 },



 /**
  * search搜索
  */
 search(){
   let searchValue = this.data.searchValue
   let page = 1
   let limit = this.data.limit
   console.log(limit)
   this.getHouseList([],limit,page,searchValue)
 },


 /**
  * write搜索框输入
  * 获取输入值
  */
 write(e){
  this.setData({
    searchValue:e.detail.value
  })
 },

 /**
  * 跳转
  * @param {*} e 
  */
 to(e){
  let type = e.currentTarget.dataset.skip
  wx.navigateTo({
   url: "/pages/"+type+'/'+type,
  })
 }
})
